<template>
  <a-modal
    :title="title"
    :width="460"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    okText="保存"
    wrapClassName="ant-modal-cust-warp">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="参数名称">
          <a-input placeholder="请输入参数名称" v-model="category_name"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="参数编号"
          hasFeedback >
          <a-input placeholder="请输入参数编号" v-model="category_code"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="序号"
          hasFeedback >
          <a-input placeholder="请输入参数序号" v-model="category_num"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="个订售价"
          hasFeedback >
          <a-input placeholder="请输入参数的个订售价" v-model="signle_price"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="团订售价"
          hasFeedback >
          <a-input placeholder="请输入参数的团订售价" v-model="team_price"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="图片上传"
          hasFeedback >
          <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-image-upload class="avatar-uploader" text="上传" v-model="fileList" style="margin-top: 8px;"></j-image-upload>
          </a-form-item>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction, postAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JImageUpload from '../../../components/jeecg/JImageUpload'

  export default {
    name: "JeecgDemoModal",
    components: {
      JImageUpload
    },
    data () {
      return {
        category_name:'',
        category_code:'',
        category_num:'0',
        signle_price:'0.0',
        team_price:'0.0',
        fileList:'',
        partentId:'',//上级定制项id
        dataDetail:[],//数据对象
        title:"操作",
        visible: false,
        model: {},
        validatorRules:{
          roleName:{
            rules: [
              { required: true, message: '请输入角色名称!' },
              { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
            ]},
          roleCode:{
            rules: [
              { required: true, message: '请输入角色名称!'},
              { min: 0, max: 64, message: '长度不超过 64 个字符', trigger: 'blur' },
              { validator: this.validateRoleCode}
            ]},
          description:{
            rules: [
              { min: 0, max: 126, message: '长度不超过 126 个字符', trigger: 'blur' }
            ]}
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        couponList: [
          {
            id: '54,55',
            name: '全部'
          },
          {
            id: '54',
            name: '男装'
          },
          {
            id: '55',
            name: '女装'
          }
        ],
        categoryList: [],
        confirmLoading: false,
        form: this.$form.createForm(this),
        url: {
          fileUpload: window._CONFIG['domianURL'] + "/sys/common/upload",
          query_detail:"/admin/parameter/query_testcategoryDetail",
          add: "/admin/parameter/add_dingzhi_canshu",
          edit: "/admin/parameter/update_dingzhi_canshu",
        },
      }
    },
    computed: {
      uploadAction: function() {
        return this.url.fileUpload;
      }
    },
    created () {

    },
    methods: {
      edit (record) {
        this.category_name='';
        this.category_num='0';
        this.remark='';
        this.model = Object.assign({}, record);
        this.visible = true;//开启弹框
        if(record.id!=null){
          this.title="编辑参数"
          this.show_categorydetail(this.model.id);
        }else {
          this.title="新增参数"
          this.partentId=record;
        }
      },
      show_categorydetail(id) {
        let params = {id:id};
        getAction(this.url.query_detail,params).then((res)=>{
          console.info(res);
          if(res.success){
            this.categorydetail = res.result;
            this.category_name=this.categorydetail.categoryName;
            this.category_code=this.categorydetail.categoryCode;
            this.category_num=this.categorydetail.numbers;
            this.signle_price=this.categorydetail.cost;
            this.team_price=this.categorydetail.teamPrice2;
            setTimeout(() => {
              this.fileList = this.categorydetail.img;
            }, 5)

          }else {
            this.$message.error("查询失败");
          }
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if(this.category_name==null || this.category_name==''){
            this.$message.warning("请输入参数名称");
            return;
          }else if(this.category_code=='' || this.category_code==null){
            this.$message.warning("请输入参数编码");
            return;
          }
          else if(this.category_num=='' || this.category_num==null){
            this.$message.warning("请输入排序");
            return;
          } else if(this.fileList=='' || this.fileList==null){
            this.$message.warning("请上传参数图片");
            return;
          }
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            let params = '';
            if(this.model.id!=null && this.model.id!=''){
              httpurl+=this.url.edit;
              method = 'put';
              params = {categoryName:this.category_name,categoryCode:this.category_code,
                cost:this.signle_price,teamPrice2:this.team_price,img:this.fileList,
                numbers:this.category_num,id:this.model.id}
            }else{
              httpurl+=this.url.add;
              method = 'post';
              params = {categoryName:this.category_name,categoryCode:this.category_code,img:this.fileList,
                numbers:this.category_num,parentId:this.partentId,topid:this.$route.query.category_id,
                cost:this.signle_price,teamPrice2:this.team_price};
            }
            httpAction(httpurl,params,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              this.category_name='';
              this.category_code='';
              this.category_num='0';
              this.signle_price='0.00';
              this.team_price='0.00';
              this.fileList='';
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>